//
//  LGPatientReportCell.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/18.
//

#import "LGPatientReportCell.h"
#import "LGCheckPhotoTool.h"
#define lineCount  (isPad?8:4)

#define itemW      (Screen_W-viewPix(88)-viewPix(8)*(lineCount-1))/lineCount

@interface LGPatientReportCell()

/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIView *backView;
/**   */
@property (nonatomic , strong)UILabel *tuberTitle;
/**   */
@property (nonatomic , strong)UILabel *tuberLabel;
/**   */
@property (nonatomic , strong)UILabel *sizeTtile;
/**   */
@property (nonatomic , strong)UILabel *sizeLabel;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)NSArray *picArray;
/**   */
@property (nonatomic , strong)NSMutableArray *photoArray;

@end

@implementation LGPatientReportCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.timeLabel.text = LGNSString(dataDic[@"createTime"]);
    NSString *tuber = LGNSString(dataDic[@"tuberTypeName"]);
    self.tuberTitle.hidden = (tuber.length>0)?NO:YES;
    self.tuberLabel.hidden = (tuber.length>0)?NO:YES;
    self.sizeTtile.hidden = (tuber.length>0)?NO:YES;
    self.sizeLabel.hidden = (tuber.length>0)?NO:YES;
    self.tuberLabel.text = tuber;
    self.sizeLabel.text = LGNSString(dataDic[@"tuberSizeName"]);
    CGFloat photoTop = (tuber.length>0)?viewPix(38):viewPix(12);
    NSArray *photoArray = [LGNSString(dataDic[@"picUrl"]) componentsSeparatedByString:@","];
    self.picArray = photoArray;
    for (UIImageView *imageView in self.photoArray) {
        [imageView removeFromSuperview];
    }
    [self.photoArray removeAllObjects];
    CGFloat left = 0;
    CGFloat top = 0;
    CGFloat maxW = (Screen_W-viewPix(88));
    for (NSInteger i=0; i<photoArray.count; i++) {
        top = (left+itemW>maxW)?top+itemW+viewPix(8):top;
        left = (left+itemW>maxW)?0:left;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, top, itemW, itemW)];
        imageView.backgroundColor = [UIColor colorWithString:@"#F3F3F3"];
        [self.photoView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i]]];
        left += itemW+viewPix(8);
        [self.photoArray addObject:imageView];
    }
    CGFloat photoH = (photoArray.count>0)?top+itemW:0;
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(photoTop);
        make.height.equalTo(@(photoH));
    }];
}

-(void)setType:(LGPositionType)type{
    [super setType:type];
    if(type == LGPositionTypeTop){
        //第一个
        [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(16));
        }];
        [self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(48));
            make.bottom.equalTo(self.baseView).offset(-viewPix(21));
        }];

    }else if (type == LGPositionTypeMiddle){
        //中间
        [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(16));
        }];
        [self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(48));
            make.bottom.equalTo(self.baseView).offset(-viewPix(5));
        }];

    }else if (type == LGPositionTypeBottom){
        //底部
        [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(32));
        }];
        [self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(64));
            make.bottom.equalTo(self.baseView).offset(-viewPix(15));
        }];

    }else{
        [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(16));
        }];
        [self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.baseView).offset(viewPix(48));
            make.bottom.equalTo(self.baseView).offset(-viewPix(16));
        }];
    }
}

-(void)checkPhotoAction{
    LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    photoView.picArray = self.picArray;
    [[ObjectTool mainWindow] addSubview:photoView];
    [photoView showViewAnimation:CGPointMake(0, 0) index:0];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.backView];
    [self.backView addSubview:self.tuberTitle];
    [self.backView addSubview:self.tuberLabel];
    [self.backView addSubview:self.sizeTtile];
    [self.backView addSubview:self.sizeLabel];
    [self.backView addSubview:self.photoView];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(48));
        make.bottom.equalTo(self.baseView).offset(-viewPix(5));
        make.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.tuberTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(viewPix(10));
        make.left.equalTo(self.backView).offset(viewPix(12));
    }];
    [self.tuberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tuberTitle.mas_right).offset(viewPix(12));
        make.centerY.equalTo(self.tuberTitle);
    }];
    [self.sizeTtile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(viewPix(160));
        make.centerY.equalTo(self.tuberTitle);
    }];
    [self.sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sizeTtile.mas_right).offset(viewPix(12));
        make.centerY.equalTo(self.sizeTtile);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(viewPix(38));
        make.left.equalTo(self.backView).offset(viewPix(12));
        make.right.equalTo(self.backView).offset(-viewPix(12));
        make.bottom.equalTo(self.backView).offset(-viewPix(12));
        make.height.equalTo(@(itemW));
    }];
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#5D5D5D" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _timeLabel;
}

-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor colorWithString:@"#F8F8F8"];
        [_backView addBorder:@"#EFEFEF" lineWidth:1 cornerRidus:10];
    }
    return _backView;
}

-(UILabel *)tuberTitle{
    if(!_tuberTitle){
        _tuberTitle = [UILabel lableWithText:@"结节类型" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _tuberTitle;
}

-(UILabel *)tuberLabel{
    if(!_tuberLabel){
        _tuberLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _tuberLabel;
}

-(UILabel *)sizeTtile{
    if(!_sizeTtile){
        _sizeTtile = [UILabel lableWithText:@"结节大小(mm)" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _sizeTtile;
}

-(UILabel *)sizeLabel{
    if(!_sizeLabel){
        _sizeLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _sizeLabel;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc]init];
        [_photoView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkPhotoAction)]];
    }
    return _photoView;
}

-(NSMutableArray *)photoArray{
    if(!_photoArray){
        _photoArray = [NSMutableArray array];
    }
    return _photoArray;
}

@end
